/*
    生成.dot
*/
import { CDG } from "./cdg";
import { statement } from "./cfg";
import { CPG } from "./cpg"; 
import * as fs from 'fs';

// console.log("digraph G {");
fs.writeFileSync('out.dot', "digraph G {\n", { encoding: 'utf-8' });

let cpg:CPG = new CPG("t.ts");
type outSta = Pick<statement, "type"|"code"|"index"|"line"|"astNode">
for(var sta of cpg.stmArray){
    const out:outSta = {
        type: sta.type,
        code: sta.code,
        index: sta.index,
        line: sta.line,
        astNode: sta.astNode
    }
    // console.log("Node"+out.index+" [label=\""+`${out.code.replace(/"/g, '\\"')}`+"\"]");
    fs.appendFileSync('out.dot', "Node"+out.index+" [label=\""+`${out.code.replace(/"/g, '\\"')}`+"\"]\n", { encoding: 'utf-8' });
}
// console.log(cpg.ed);//边列表
for(var edge of cpg.ed){
    // console.log("Node"+edge.u+" -> Node"+edge.v)
    if(edge.type == 0)fs.appendFileSync('out.dot', "Node"+edge.u+" -> Node"+edge.v+"  [label=\"cfg\",style=\"bold\"]\n", { encoding: 'utf-8' });
    else if(edge.type == 1)fs.appendFileSync('out.dot', "Node"+edge.u+" -> Node"+edge.v+"  [label=\"cdg\",style=\"dotted\"]\n", { encoding: 'utf-8' });
    else if(edge.type == 2)fs.appendFileSync('out.dot', "Node"+edge.u+" -> Node"+edge.v+"  [label=\"ddg : "+edge.ident+"\",style=\"dashed\"]\n", { encoding: 'utf-8' });
}

fs.appendFileSync('out.dot', "}\n", { encoding: 'utf-8' });
